/* eslint-disable linebreak-style,no-shadow */
const fs = require('fs');
const path_ = require('path');

const spawn = eval('require')('child_process').spawn;

const walk = function (dir, childPath) {
    const children = [];
    childPath = childPath || '';
    fs.readdirSync(dir).forEach((filename) => {
        const path = path_.join(dir, filename);// dir+"/"+filename
        const stat = fs.statSync(path);
        if (stat && stat.isDirectory()) {
            children.push({
                childPath: path_.join(childPath, filename), // childPath + '/' + filename + '/',
                filename,
                path,
                relatedPath: childPath,
                isDir: true,
                size: 0,
            });
            children.splice(children.length, 0, ...walk(path, path_.join(childPath, filename)));
        } else {
            children.push({
                filename,
                childPath: `${path_.join(childPath, filename)}`,
                path,
                relatedPath: childPath,
                isDir: false,
                size: stat.size,
            });
        }
    });
    return children;
};

function f(path, bakName) {
    bakName = bakName || 'bak';
    const dear = (files) => {
        const matlabCode = `function split_all(tifName,outName)
    [img,R] = geotiffread(tifName);
    geotiffwrite(outName,img(:,:,1:3),R);
end`;
        console.log(files);
        files = files.filter(f => f.path.substring(f.path.length - 4) === '.tif')
            .map(f => f.path);
        const matlabDear = files.map((f) => {
            return `split_all('${f}.${bakName}.tif','${f}')`;
        });
        const rename = files.map((f) => {
            return `ren "${f}" "${f.split('\\').pop()}.${bakName}.tif"`;
        });
        console.log(rename.join('\n'));
        console.log('\n\n');
        console.log(matlabDear.join('\n'));
        fs.writeFileSync('test.txt', `请先重命名文件，为了备份，在执行matlab语句。matlab 需要单独将一个 split_all.m 文件，保存以下代码。\n${matlabCode}\n\n\n${rename.join('\r\n')}\n\n\n${matlabDear.join('\r\n')}`);
        spawn('notepad test.txt', {
            shell: true,
        });
        setTimeout(() => process.exit(), 1000);
    };
    dear(walk(path));
}

// f('C:\\Users\\HUZENGYUN\\Desktop\\ur_cropland_ex');

// if (process.argv.length > 2) {
//     console.log(`处理文件夹 ${process.argv[2]}`);
//     f(process.argv[2], process.argv[3]);
// } else {
//     console.log(`使用方法 node dear 路径 [bak名称]
//     路径是必须的，bak名称可以修改
//     例如
//     node dear c:\\data\\tifs
//     node dear c:\\data\\tifs backup`);
//     throw new Error('没有路径，参数错误');
// }

function getFiles() {
    const txtFile = 'C:\\Users\\HUZENGYUN\\Desktop\\test\\line\\train_line.txt';
    const files = walk('C:\\Users\\HUZENGYUN\\Desktop\\test\\ur_cropland_ex')
        .filter(f => f.path.substring(f.path.length - 4) === '.tif')
        .map(f => f.path.replace(/\\\\/g, '\\'))
        .map((f) => {
            const s = path_.parse(f);
            return [f, `C:\\Users\\HUZENGYUN\\Desktop\\test\\line\\${s.name}_Line.tif`].join(' ');
        }).join('\n');
    fs.writeFileSync(txtFile, files, 'utf-8');
}

// getFiles();


const files = walk('C:\\Users\\HUZENGYUN\\Documents\\git\\tianqi\\wegq')
    .filter(f => f.path.substring(f.path.length - 4) === '.tif')
    .map(f => f.path);

console.log(`"${files.join('|')}|"`);

